package com.ruoyi.aitcommon.vo;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitcommon.utils.ExceptionUtils;
import com.ruoyi.aitcommon.utils.ObjUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class AggMap extends LinkedHashMap<String,Object> {
	private final String headname="headVO";
	private final String bodyname="bodyVOS";
	public AggMap() {
		super();
	}
	public AggMap(Map<String,Object> map) {
		super();
		this.putAll(map);
	}
	public AggMap(Map<String,Object> head,List<Map<String,Object>> bodys){
		super();
		if(head!=null)
			this.put(this.headname,head);
		if(bodys!=null)
			this.put(this.bodyname,bodys);
	}
	public Map<String,Object> getHeadVO(){
		if(this.containsKey(this.headname))
			return (Map<String,Object>)this.get(this.headname);
		else
			return null;
	}
	public void setHeadVO(Map<String,Object> head){
		this.put(this.headname,head);
	}
	public List<Map<String,Object>> getBodyVOS(){
		if(this.containsKey(this.bodyname))
			return (List<Map<String,Object>>)this.get(this.bodyname);
		else
			return null;
	}
	public void setBodyVOS(List<Map<String,Object>> bodys){
		this.put(this.bodyname,bodys);
	}

	/**
	 * 将AggMap对象转换成Map对象
	 * @param head 头对象名称,可以为空,为空时将头对象属性添加到返回对象中
	 * @param body 行对象名称,必填
	 * @return Map对象
	 */
	public Map<String,Object> toMap(String head,String body) throws Exception {
		ExceptionUtils.checkBlankOrUndefined(body, "AggMap对象转换成Map对象时,行对象名称不能为空");
		Map<String,Object> retmap=new LinkedHashMap<>();
		if(ObjUtils.isNotBlank(head)){
			retmap.put(head, this.getHeadVO());
		}else{
			retmap.putAll(this.getHeadVO());
		}
		retmap.put(body, this.getBodyVOS());
		return retmap;
	}

	/**
	 * 通过JSON深度Clone对象
	 * @return Clone后对象
	 */
	@Override
	public AggMap clone() {
		String str = JSONObject.toJSONString(this);
		final AggMap retmap = JSONObject.parseObject(str, AggMap.class);
		return retmap;
	}

}
